import queue
import random
import re
import time

import pymysql as pymysql
from fake_useragent import UserAgent
from lxml import etree
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


class basketballFull():
    chrome_path = r'./chromedriver.exe'

    def __init__(self, url):
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        self.q = queue.Queue()
        self.win1_q = queue.Queue()
        self.simple_id_q = queue.Queue()
        self.driver = webdriver.Chrome()
        self.base_url = url
        self.__host = '81.69.226.125'
        self.__port = 4510
        self.__user = 'root'
        self.__password = 'root'
        self.__db = 'titan007'
        self.__charset = 'utf8mb4'
        self.lists = []
        self.dicts = {}
        self.windows_1 = 0
        self.windows_2 = 0
        self.simple_ids = []
        self.s = ""
        self.ua = UserAgent()
        self.__conn = pymysql.connect(
            host=self.__host,
            port=self.__port,
            user=self.__user,
            password=self.__password,
            db=self.__db,
            charset=self.__charset
        )

    def sql_operate(self, lists):
        cursor = self.__conn.cursor()
        for single in lists:
            # for key, value in single.items():
            #     if not value:
            #         value = 'null'
            #         single[key] = value
            sql_inert = "insert into tp_game values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
            data = [(single['id'], single['game_type'], single['match_name'], single['match_date'], single['home_team'],
                     single['away_team'], single['half_score'], single['final_score'], single['match_status'],
                     single['minutes'],
                     single['fx_id'], single['hc'], single['hc_home'], single['hc_away'], single['ou'], single['ou_o'],
                     single['ou_u'],
                     single['hc_half'], single['hc_home_half'], single['hc_away_half'], single['ou_half'],
                     single['ou_o_half'],
                     single['ou_u_half'], single['style'], single['sort'], single['create_time'], single['update_time'],
                     single['tag'])]
            print(data)
            print('正在插入中')
            cursor.executemany(sql_inert, data)
            self.__conn.commit()
        cursor.close()
        self.__conn.close()

    def get_html(self):
        self.driver.get(self.base_url)
        self.driver.find_element(By.XPATH, '//*[@id="buttonImportant"]/a').click()
        self.driver.find_element(By.XPATH, '//*[@id="selectCompany"]/option[4]').click()
        time.sleep(random.randint(1, 2))
        complete_data = self.driver.page_source
        return complete_data

    # 解析出详情页url
    def parse_detail_url(self, complete_data):
        complete_html = etree.HTML(complete_data)
        total_table = complete_html.xpath('//*[@id="live"]/table')
        total_tbody = complete_html.xpath('//table[@class="score-table"]/tbody')
        suspend_id = complete_html.xpath('//table[@class="score-table"]/tbody/tr[2]/td[15]/@id')
        window_1 = complete_html.xpath('//table[@class="score-table"]/tbody/tr[2]/@id')
        return total_tbody, suspend_id, window_1, total_table

    # 组装数据
    def assemble_data(self, total_table):
        for table in total_table:
            if not table.xpath('@style'):
                simple_total_tbody = table.xpath('tbody')
                simple_total_tbody_id = table.xpath('@id')
                table_id = simple_total_tbody_id
                for tbody in simple_total_tbody:
                    is_td_leng = len(tbody.xpath('tr[2]/td'))
                    match_name = "".join(tbody.xpath('tr[2]/td[2]/span/a/text()'))
                    if not match_name:
                        match_name = "".join(tbody.xpath('tr[2]/td[2]/span/text()'))
                    match_date = "".join(tbody.xpath('tr[2]/td[3]/text()'))
                    home_team_1 = "".join(tbody.xpath('tr[2]/td[4]/a/text()'))
                    home_team_2 = "".join(tbody.xpath('tr[2]/td[4]/span/span/text()'))
                    home_team = home_team_1 + home_team_2
                    away_team_1 = "".join(tbody.xpath('tr[3]/td[1]/a/text()'))
                    away_team_2 = "".join(tbody.xpath('tr[3]/td[1]/span/span/text()'))
                    away_team = away_team_1 + away_team_2
                    match_status = "".join(tbody.xpath('tr[1]/td[2]/span/span[1]/text()'))
                    minutes = "".join(tbody.xpath('tr[1]/td[2]/span/span[2]/text()'))
                    home_half_score_1 = "".join(tbody.xpath('tr[2]/td[9]/font[1]/text()'))
                    home_half_score_2 = "".join(tbody.xpath('tr[2]/td[9]/text()'))
                    home_half_score_3 = "".join(tbody.xpath('tr[2]/td[9]/font[2]/text()'))
                    home_half_score = home_half_score_1 + home_half_score_2 + home_half_score_3
                    away_half_score_1 = "".join(tbody.xpath('tr[3]/td[6]/font[1]/text()'))
                    away_half_score_2 = "".join(tbody.xpath('tr[3]/td[6]/text()'))
                    away_half_score_3 = "".join(tbody.xpath('tr[3]/td[6]/font[2]/text()'))
                    away_half_score = "-" + away_half_score_1 + away_half_score_2 + away_half_score_3
                    half_score = home_half_score + "," + away_half_score
                    home_final_score = "".join(tbody.xpath('tr[2]/td[10]/text()'))
                    away_final_score = "".join(tbody.xpath('tr[3]/td[7]/text()'))
                    final_score = home_final_score + ',' + '-' + away_final_score
                    hc = "".join(tbody.xpath('tr[2]/td[14]/a/span[1]/text()'))
                    if not hc:
                        hc = "-" + "".join(tbody.xpath('tr[3]/td[11]/a/span[1]/text()'))
                    hc_home = "".join(tbody.xpath('tr[2]/td[14]/a/span[2]/text()'))
                    hc_away = "".join(tbody.xpath('tr[3]/td[11]/a/span[2]/text()'))
                    ou = "".join(tbody.xpath('tr[2]/td[15]/a/text()')).strip()
                    ou_o = "".join(tbody.xpath('tr[2]/td[15]/a/span/text()'))
                    ou_u = "".join(tbody.xpath('tr[3]/td[12]/a/span/text()'))
                    if is_td_leng != 16:
                        home_half_score_1 = "".join(tbody.xpath('tr[2]/td[10]/font[1]/text()'))
                        home_half_score_2 = "".join(tbody.xpath('tr[2]/td[10]/text()'))
                        home_half_score_3 = "".join(tbody.xpath('tr[2]/td[10]/font[2]/text()'))
                        home_half_score = home_half_score_1 + home_half_score_2 + home_half_score_3
                        away_half_score_1 = "".join(tbody.xpath('tr[3]/td[7]/font[1]/text()'))
                        away_half_score_2 = "".join(tbody.xpath('tr[3]/td[7]/text()'))
                        away_half_score_3 = "".join(tbody.xpath('tr[3]/td[7]/font[2]/text()'))
                        away_half_score = "-" + away_half_score_1 + away_half_score_2 + away_half_score_3
                        half_score = home_half_score + "," + away_half_score
                        home_final_score = "".join(tbody.xpath('tr[2]/td[11]/text()'))
                        away_final_score = "".join(tbody.xpath('tr[3]/td[8]/text()'))
                        final_score = home_final_score + ',' + '-' + away_final_score
                        hc = "".join(tbody.xpath('tr[2]/td[15]/a/span[1]/text()'))
                        if not hc:
                            hc = "-" + "".join(tbody.xpath('tr[3]/td[12]/a/span[1]/text()'))
                        hc_home = "".join(tbody.xpath('tr[2]/td[15]/a/span[2]/text()'))
                        hc_away = "".join(tbody.xpath('tr[3]/td[12]/a/span[2]/text()'))
                        ou = "".join(tbody.xpath('tr[2]/td[16]/a/text()')).strip()
                        ou_o = "".join(tbody.xpath('tr[2]/td[16]/a/span/text()'))
                        ou_u = "".join(tbody.xpath('tr[3]/td[13]/a/span/text()'))
                    tr_id = str("".join(table_id)).replace("table", "tr1")
                    is_hc_half_path = '//*[@id="{}"]/td[16]/a[2]/text()'.format(tr_id)
                    is_ou_half_path = '//*[@id="{}"]/td[16]/a[3]/text()'.format(tr_id)
                    is_win_1_path = "".join(tbody.xpath(is_hc_half_path))
                    hc_home_half, hc_half, hc_away_half = self.get_hc_half_selenium_contorl(tr_id, is_win_1_path)
                    is_win_2_path = "".join(tbody.xpath(is_ou_half_path))
                    ou_o_half, ou_half, ou_u_half = self.get_ou_half_selenium_contorl(tr_id, is_win_2_path)
                    times = str(time.time())[:10]
                    self.dicts = {
                        "id": str(table_id).replace("btotal_", ""),
                        "game_type": 2,
                        "match_name": match_name,
                        "match_date": match_date,
                        "home_team": home_team_1,
                        "away_team": away_team_1,
                        "half_score": half_score,
                        "final_score": final_score,
                        "match_status": 1,
                        "minutes": minutes,
                        "fx_id": 1,
                        "hc": hc,
                        "hc_home": hc_home,
                        "hc_away": hc_away,
                        "ou": ou,
                        "ou_o": ou_o,
                        "ou_u": ou_u,
                        "hc_half": hc_half,
                        "hc_home_half": hc_home_half,
                        "hc_away_half": hc_away_half,
                        "ou_half": ou_half,
                        "ou_o_half": ou_o_half,
                        "ou_u_half": ou_u_half,
                        "style": 0,
                        "sort": 1,
                        "create_time": times,
                        "update_time": times,
                        "tag": 0
                    }
                    print(self.dicts)
                    self.lists.append(self.dicts)
        self.driver.close()
        return self.lists

    # 抓取ou_half
    def get_ou_half_selenium_contorl(self, tr_id, is_win_2_path):
        if is_win_2_path:
            win2_xpath = '//*[@id="{}"]/td[16]/a[3]'.format(tr_id)
        else:
            win2_xpath = '//*[@id="{}"]/td[17]/a[3]'.format(tr_id)
        self.driver.find_element(By.XPATH, win2_xpath).click()
        self.driver.switch_to.window(self.driver.window_handles[1])
        self.driver.find_element(By.XPATH, '//*[@id="tabs"]/a[2]').click()
        self.driver.implicitly_wait(10)
        complete_data = self.driver.page_source
        pattern = re.compile(
            '''36\*.*?</td>.*?<td.*?oddstype="wholeLastOdds">.*?">(.*?)</font>.*?<td id=".*?" oddstype="wholeLastOdds">(.*?)</td>.*?<font color=".*?">(.*?)</font>''',
            re.S)
        ou_halfs = re.findall(pattern, complete_data)
        if ou_halfs:
            ou_o_half = str(ou_halfs[0][0]).strip()
            if ou_o_half == '&nbsp;':
                ou_o_half = ''
            ou_half = str(ou_halfs[0][1]).strip()
            if ou_half == '&nbsp;':
                ou_half = ''
            ou_u_half = str(ou_halfs[0][2]).strip()
            if ou_u_half == '&nbsp;':
                ou_u_half = ''
        else:
            ou_o_half = ''
            ou_half = ''
            ou_u_half = ''
        self.driver.close()
        self.driver.switch_to.window(self.driver.window_handles[0])
        return ou_o_half, ou_half, ou_u_half

    # hc_half窗口
    def get_hc_half_selenium_contorl(self, tr_id, is_win_1_path):
        if is_win_1_path:
            print(tr_id)
            win1_xpath = '//*[@id="{}"]/td[16]/a[2]'.format(tr_id)
        else:
            win1_xpath = '//*[@id="{}"]/td[17]/a[2]'.format(tr_id)
        self.driver.find_element(By.XPATH, win1_xpath).click()
        self.driver.implicitly_wait(10)
        self.driver.switch_to.window(self.driver.window_handles[1])
        self.driver.find_element(By.XPATH, '//*[@id="tabs"]/a[2]').click()
        self.driver.implicitly_wait(10)
        complete_data = self.driver.page_source
        pattern = re.compile(
            '''36\*.*?</td>.*?<td.*?oddstype="wholeLastOdds">.*?">(.*?)</font>.*?<td id=".*?" oddstype="wholeLastOdds">(.*?)</td>.*?<font color=".*?">(.*?)</font>''',
            re.S)
        hc_halfs = re.findall(pattern, complete_data)
        if hc_halfs:
            hc_home_half = str(hc_halfs[0][0]).strip()
            if hc_home_half == '&nbsp;':
                hc_home_half = ''
            hc_half = str(hc_halfs[0][1]).strip()
            if hc_half == '&nbsp;':
                hc_half = ''
            hc_away_half = str(hc_halfs[0][2]).strip()
            if hc_away_half == '&nbsp;':
                hc_away_half = ''
        else:
            hc_home_half = ''
            hc_half = ''
            hc_away_half = ''
        self.driver.close()
        self.driver.switch_to.window(self.driver.window_handles[0])
        return hc_home_half, hc_half, hc_away_half

    def main(self):
        complete_data = self.get_html()
        total_tbody, suspend_id, window_1s, total_table = self.parse_detail_url(complete_data)
        lists = self.assemble_data(total_table)
        self.sql_operate(lists)


if __name__ == '__main__':
    run = basketballFull('http://lq3.titan007.com/nba.htm')
    run.main()
